<?php
	class RequestAction extends Action{
		public function getMajorList(){
			$list=M("Codetoname");
			$data=$list->where("type='major'")->field("code,name")->select();
			echo json_encode($data);
		}
		
		public function getClassList(){
			if (isset($_POST["getmajor"])&&(int)$_POST["getmajor"]!=0) {
				$major=$_POST["getmajor"];
				unset($_POST["getmajor"]);
			}else{
				exit();
			}
			$list=M("Codetoname");
			$where["type"]=(int)$major;
			$data=$list->where($where)->field("code,name")->select();
			echo json_encode($data);
		}
		public function dealBy(){
			$by=M("Baseinfo");
			if($_POST["sid"]=="all"){
				$where["grade"]=(int)$_POST["getgrade"];
				$where["major"]=(int)$_POST["getmajor"];
				$where["class"]=(int)$_POST["getclass"];
				$sid=$by->where($where)->field("sid")->select();
				$count=count($sid);
				$year=date("Y");
				$error=0;
				for($i=0;$i<$count;$i++){
					if($_POST["is"]=="true"){
						$where["sid"]=$sid[$i]["sid"];
						$result=$by->where($where)->setField("isby", $year);
						if((string)$result=="false"){
							$error++;
						}
					}else{
						$where["sid"]=$sid[$i]["sid"];
						$result=$by->where($where)->setField("isby", 0);
						if((string)$result=="false"){
							$error++;
						}
					}
				}
				if($error>0){
					echo "error";
				}else{
					echo "success";
				}
				
			}else{
				if($_POST["is"]=="true"){
					$year=date("Y");
					$where["sid"]=$_POST["sid"];
					$result=$by->where($where)->setField("isby", $year);
					if($result>=0){
						echo "success";
					}else{
						echo "error";
					}
				}else{
					$where["sid"]=$_POST["sid"];
					$result=$by->where($where)->setField("isby", 0);
					if($result>=0){
						echo "success";
					}else{
						echo "error";
					}
				}
			}
			
		}

		public function dealByDetail($sid=""){
			header("Content-type: text/html; charset=utf-8"); 
			if(empty($sid)){
				echo '<center><font color="red">参数错误</center>';
				exit;
			}
			$data["workplace"]=$_POST["workplace"];
			$data["job"]=$_POST["job"];
			$data["address"]=$_POST["address"];
			$data["phone"]=$_POST["phone"];
			$data["wordtoschool"]=$_POST["word"];
			$data["remark"]=$_POST["remark"];
			$by=M("Graduate");
			$count=$by->where("sid=".$sid)->count();
			if ($count==1) {
				$result=$by->where("sid=".$sid)->save($data);
			}else{
				$data["sid"]=$sid;
				$result=$by->add($data);
			}
			if($result>=0){
				echo '<center><font color="green">毕业生信息编辑成功</center>';
			}else{
				echo '<center><font color="red">毕业生信息编辑失败，请检查</center>';
			}
		}
		
		public function uploadPic($type="display"){
			header("Content-type: text/html; charset=utf-8");
			$st=M("Baseinfo");
			if($type=="display"){
				$sid=$_GET["sid"];
				if (empty($sid)){
					$this->assign("color","red");
					$this->assign("sid","请输入先学号<br>图片大小小于1M");
					$this->display("Page:uploadPic");
				}else{
					$pic=$st->where("sid=".$sid)->getField("pic");
					if(empty($pic)){
						$this->assign("color","green");
						$this->assign("sid",$sid);
						$this->display("Page:uploadPic");
					}else{
						$this->show('<center><img width="180" height="220" src="__ROOT__/stu_pic/'.$pic.'" /></center>');
					}
					
				}
			}else{
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize  = 3145728 ;// 设置附件上传大小
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->savePath =  './stu_pic/';// 设置附件上传目录
				if(!$upload->upload()) {// 上传错误提示错误信息
					echo '<font color="red">'.$upload->getErrorMsg().'</font>';
				}else{// 上传成功 获取上传文件信息
					$newSID=$_POST["newSID"];
					$info =  $upload->getUploadFileInfo();
					$count=$st->where("sid=".$newSID)->count();
					if ($count==1){
						$result=$st->where("sid=".$newSID)->setField("pic", $info[0]["savename"]);
					}else{
						$data["sid"]=$newSID;
						$data["pic"]=$info[0]["name"];
						$result=$st->add($data);
					}
					if($result>=0){
						$this->show('<center><img width="200" height="250" src="__ROOT__/stu_pic/'.$info[0]["savename"].'" /></center>');
					}else{
						echo '<center><font color="red">照片上传失败</font></center>';
					}
				}
				
				
			}
		}
		public function checkPass($sid=""){
			if (empty($sid)) {
				echo "参数错误";
				exit();
			}
			$oldpass=$_POST["getoldpass"];
			$newpass=$_POST["getnewpass"];
			if (empty($oldpass) || empty($newpass)) {
				echo "请填写完整再提交!";
				exit();
			}
			
			$pass=M("Baseinfo");
			$old=$pass->where("sid=".$sid)->getField("password");
			if (md5($oldpass)==$old) {
				$result=$pass->where("sid=".$sid)->setField("password", md5($newpass));
				if ($result>=0) {
					echo "密码修改成功";
				}else{
					echo "密码修改失败!";
				}
			}else{
				echo "原来的密码错误，请检查!";
			}
		}
		
	}

?>
